package stackdemo;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2025-01-24
 * Time: 17:00
 */
public class MinStack {
    Stack<Integer> stack;
    Stack<Integer> minStack;

    public MinStack(){
        stack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push (int val){
        stack.push(val);
        if (minStack.empty()){
            minStack.push(val);
        }else {
            int peekMinVal = minStack.peek();
            if (val <= peekMinVal){
                minStack.push(val);
            }
        }
    }

    public void pop(){
        int val = stack.pop();

        if (val == minStack.peek()){
            minStack.pop();
        }
    }

    //peek
    public int top(){
        return stack.peek();
    }

    //O(1)
    public int getMin(){
        return minStack.peek();
    }
}
